package org.jeecg.modules.operations.assistant.xcbd.service.impl;

import org.jeecg.modules.operations.assistant.xcbd.entity.XcbdSalesContract;
import org.jeecg.modules.operations.assistant.xcbd.entity.XcbdWorkflow;
import org.jeecg.modules.operations.assistant.xcbd.mapper.XcbdSalesContractMapper;
import org.jeecg.modules.operations.assistant.xcbd.mapper.XcbdWorkflowMapper;
import org.jeecg.modules.operations.assistant.xcbd.service.IXcbdWorkflowService;
import org.springframework.stereotype.Service;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Description: 工作流程
 * @Author: jeecg-boot
 * @Date:   2024-12-21
 * @Version: V1.0
 */
@Service
public class XcbdWorkflowServiceImpl extends ServiceImpl<XcbdWorkflowMapper, XcbdWorkflow> implements IXcbdWorkflowService {
	
	@Autowired
	private XcbdWorkflowMapper xcbdWorkflowMapper;

	@Autowired
	private XcbdSalesContractMapper xcbdSalesContractMapper;
	
	@Override
	public List<XcbdWorkflow> selectByMainId(String mainId) {
		return xcbdWorkflowMapper.selectByMainId(mainId);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void approval(XcbdWorkflow xcbdWorkflow, Integer step) {
		XcbdSalesContract salesContract = xcbdSalesContractMapper.selectById(xcbdWorkflow.getId());
		XcbdWorkflow workflow = new XcbdWorkflow();
		workflow.setWorkOrderId(xcbdWorkflow.getId());
		workflow.setStatus("不同意");
		if(xcbdWorkflow.getStatus().equals("1")){
			workflow.setStatus("同意");
		}

		if(step==1){
			if(salesContract.getContractAmount().doubleValue() > 50000){
				workflow.setContent("副总经理审批"+workflow.getStatus()+"，超过五万待总经理审批");
				salesContract.setProcessInitiator("林泽鑫");
				salesContract.setProcessStatus("2");  //待总经理审批
				if(!xcbdWorkflow.getStatus().equals("1")){
					salesContract.setContractStatus(2);  // 作废
				}
			} else {
				workflow.setContent("超过五千小于等于五万副总经理审批完毕，"+workflow.getStatus());
				salesContract.setContractStatus(1);
				salesContract.setProcessInitiator("");
				salesContract.setProcessStatus("3");  //审批完成
				if(!xcbdWorkflow.getStatus().equals("1")){
					salesContract.setContractStatus(2);  // 作废
				}
			}
		}
		if(step==2){
			workflow.setContent("超过五万总经理审批"+workflow.getStatus());
			salesContract.setContractStatus(1);
			salesContract.setProcessInitiator("");
			salesContract.setProcessStatus("3");  //总经理审批完成
			if(!xcbdWorkflow.getStatus().equals("1")){
				salesContract.setContractStatus(2);  // 作废
			}
		}
		xcbdSalesContractMapper.updateById(salesContract);
		xcbdWorkflowMapper.insert(workflow);
	}
}